
package com.kugou.game.sdk.protocol;

import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.game.sdk.entity.Coupon;
import com.kugou.game.sdk.entity.CouponListResponse;
import com.kugou.game.sdk.utils.ProtocolCmd;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 描述: 请求历史代金券列表协议类
 *
 * @author jamywang
 * @since 2016年5月18日 下午2:35:41
 */
public class RequestHistoryCouponTask {

    private ArrayList<String> mCmdList = new ArrayList<String>();

    public CouponListResponse request(int page, int pageSize, String userName) {
        HashMap<String, Object> params = new HashMap<String, Object>();
        ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();
        HttpBaseRequestPackage requestPackage = new HttpBaseRequestPackage();
        ResponsePackage responsePackage = new HistoryCouponListResponsePackage();
        CouponListResponse response = new CouponListResponse();
        params.put("cmd", ProtocolCmd.CMD_HISTORY_COUPON_LIST);
        params.put("username", userName);
        params.put("page", page);
        params.put("size", pageSize);
        requestParams.add(params);
        mCmdList.add(ProtocolCmd.CMD_HISTORY_COUPON_LIST);
        requestPackage.setRequestParams(requestParams);
        try {
            KGHttpClient.request(requestPackage, responsePackage);
            responsePackage.getResponseData(response);
            return response;
        } catch (Exception e) {
            // TODO: handle exception
        }
        return null;
    }

    private class HistoryCouponListResponsePackage extends
            HttpBaseResponsePackage<CouponListResponse> {

        @Override
        protected void handleResponse(CouponListResponse response, String responseJsonStr) {
            try {
                LogUtil.d("response", "RequestHistoryCouponTask:" + responseJsonStr);
                JSONArray responseArray = new JSONArray(responseJsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {
                    if (i >= mCmdList.size()) {
                        break;
                    }
                    JSONObject jsonResp = responseArray.optJSONObject(i);
                    String cmd = jsonResp.optString("cmd");
                    String code = jsonResp.optString("code");
                    String message = jsonResp.optString("message");
                    String prompt = jsonResp.optString("prompt");
                    response.setCode(code);
                    response.setMessage(message);
                    response.setPrompt(prompt);
                    if (jsonResp.optInt("code") != 1) {
                        continue;
                    }
                    if (jsonResp.isNull("data")) {
                        continue;
                    }
                    if (ProtocolCmd.CMD_HISTORY_COUPON_LIST.equalsIgnoreCase(cmd)) {
                        JSONArray data = jsonResp.optJSONArray("data");
                        if (data != null) {
                            ArrayList<Coupon> coupons = new ArrayList<Coupon>();
                            for (int j = 0; j < data.length(); j++) {
                                JSONObject obj = data.optJSONObject(j);
                                Coupon coupon = new Coupon();
                                coupon.setMoney(obj.optInt("money"));
                                coupon.setCouponName(obj.optString("couponname"));
                                coupon.setPlatform(obj.optInt("platform"));
                                coupon.setType(obj.optInt("paytype"));
                                coupon.setEndtime(obj.optLong("endtime") * 1000);
                                coupon.setStatus(obj.optInt("status"));
                                JSONArray gamesData = obj.optJSONArray("game");
                                if (gamesData != null) {
                                    ArrayList<String> games = new ArrayList<String>();
                                    for (int k = 0; k < gamesData.length(); k++) {
                                        games.add(gamesData.optJSONObject(k).optString("gamename"));
                                    }
                                    coupon.setGames(games);
                                }
                                coupons.add(coupon);
                            }
                            response.setOk(true);
                            response.setCouponList(coupons);
                        }
                    }

                }
            } catch (Exception e) {
            }

        }
    }

}
